package com.jf3q.sup.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jf3q.sup.domain.ProductCategories;
import com.jf3q.sup.domain.Products;
import com.jf3q.sup.dto.ProductsDto;
import com.jf3q.sup.service.ProductCategoriesService;
import com.jf3q.sup.vo.ProductsVo;
import com.jf3q.sup.vo.ResultVo;
import com.jf3q.sup.service.ProductsService;
import com.jf3q.sup.vo.products.ProductsEchartsVo;
import com.jf3q.sup.vo.products.StockVo;
import org.apache.ibatis.jdbc.RuntimeSqlException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.sql.SQLIntegrityConstraintViolationException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/products")
public class ProductsController {
    @Autowired
    ProductsService productsService;
    @Autowired
    ProductCategoriesService productCategoriesService;

    @GetMapping("getEcharts")
    public ResultVo getEcharts() {
        ProductsEchartsVo productsEchartsVo = new ProductsEchartsVo();
        try {
            productsEchartsVo = productsService.getEcharts();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return ResultVo.success("成功", productsEchartsVo);
    }
    /**
     * 获取商品详细信息
     * @param productsDto 商品分类和详情信息
     * @return 商品详细信息结果
     */
    @PostMapping("/getPage")
    public ResultVo getPage(@RequestBody ProductsDto productsDto) {
        try {
            Page<ProductsVo> productsVoPage = productsService.ClassificationAndDetails(productsDto);
            return ResultVo.success("商品详细信息", productsVoPage);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 添加商品详细信息
     * @param productsVo 商品详细信息
     * @return 添加商品详细信息结果
     */
    @PostMapping("/create")
    public ResultVo create(@RequestBody ProductsVo productsVo) {
        try {
            Products products = new Products();
            // 将商品详细信息VO中的属性复制到商品实体中
            BeanUtils.copyProperties(productsVo, products);
            products.setStockQuantity(0);
            products.setSuppliersId(productsVo.getSupplierId());
            products.setCreatedAt(new Date());
            products.setUpdatedAt(new Date());
            productsService.save(products);
            return ResultVo.success("添加商品详细信息成功", null);
        } catch (BeansException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 修改商品详细信息
     * @param productsVo 商品详细信息
     * @return 修改商品详细信息结果
     */
    @PutMapping("/update")
    public ResultVo update(@RequestBody ProductsVo productsVo) {
        try {
            Products products = new Products();
            BeanUtils.copyProperties(productsVo, products);
            products.setPurchasingPrice(BigDecimal.valueOf(0.0));
            products.setSuppliersId(productsVo.getSupplierId());
            productsService.update(products, new LambdaQueryWrapper<Products>().eq(Products::getId, productsVo.getId()));
            return ResultVo.success("修改商品详细信息成功", null);
        } catch (BeansException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 删除商品详细信息
     * @param id 商品详细信息
     * @return 删除商品详细信息结果
     */
    @DeleteMapping("/delete/{id}")
    public ResultVo delete(@PathVariable Integer id) {
        try {
            productsService.removeById(id);
            return ResultVo.success("删除商品详细信息成功", null);
        }catch (Exception e) {
            throw new RuntimeException("此信息数据与其他数据相连，请先删除与其他关联的数据再进行操作！");
        }
    }


    @PostMapping("/stockWithEcharts")
    public ResultVo stockWithEcharts(){
        List<StockVo> stockVoList= null;
        try {
            List<Products> productsList = productsService.list();
            stockVoList = new ArrayList<>();
            for (Products products : productsList) {
                StockVo stockVo = new StockVo();
                BeanUtils.copyProperties(products,stockVo);
                stockVoList.add(stockVo);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return ResultVo.success("",stockVoList);
    }

}
